package com.guoq.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.guoq.pojo.User;
import com.guoq.pojo.UserExample;
import com.guoq.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private IUserService userService;
	
	@RequestMapping("/showUser")
	public String toIndex(HttpServletRequest request,Model model){
		return "/index";
	}
	
	@RequestMapping("/getUserById")
	public String getUserById(HttpServletRequest request,Model model){
		int userId = Integer.parseInt(request.getParameter("id"));
		User user = this.userService.getUserById(userId);
		model.addAttribute("user", user);
		return "/index";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public List<User> list(HttpServletRequest request,Model model){
		
		List<User> list = this.userService.getUserList();
		return list;
	}
	
	@RequestMapping("/pagelist")
	@ResponseBody
	public List<User> pagelist(UserExample example){
		PageHelper.startPage(1, 3); // 核心分页代码
		List<User> list = this.userService.selectByExample(example);
		return list;
	}
}
